﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using TTUnityPlugin;
namespace TTGame {

/// <summary>
/// 实现 Log LogError LogWarning
/// 可自行扩展 (注意 两个bool变量的使用)
/// </summary>

    public enum DEBUGLEVEL : int
    {
        DEBUG = 0,
        INFO,
        WARNING,
        ERROR,
        NOLOG,  // 放在最后面，使用这个时表示不输出任何日志（!!!慎用!!!）
    }

public static class DebugUtil {

    public static DEBUGLEVEL debugLevel = DEBUGLEVEL.DEBUG;
    private static DEBUGLEVEL writeLevel = DEBUGLEVEL.NOLOG;
    private static string logFileName = string.Empty;

    static string getLogPrefix() {
        return string.Format("{0}({1}) | ", System.DateTime.Now.ToString("HH:mm:ss.fff"), Time.time);
    }

    public static bool IsLogOpen() {
        return debugLevel <= DEBUGLEVEL.DEBUG;
    }

    public static void Log(object message) {
        if(debugLevel > DEBUGLEVEL.INFO)
            return;

        var context = getLogPrefix() + message;
        Debug.Log(context);
        writeMessageToFile(context, DEBUGLEVEL.INFO);
    }

    public static void LogWarning(object message) {
        if(debugLevel > DEBUGLEVEL.WARNING)
            return;

        var context = getLogPrefix() + message;
        Debug.LogWarning(context);
        writeMessageToFile(context, DEBUGLEVEL.WARNING);
    }

    public static void LogError(object message) {
        if(debugLevel > DEBUGLEVEL.ERROR)
            return;

        var context = getLogPrefix() + message;
        Debug.LogError(context);
        writeMessageToFile(context, DEBUGLEVEL.ERROR);
    }

    public static void LogFormat(string format, params object[] args) {
        if(debugLevel > DEBUGLEVEL.INFO)
            return;

        string context = string.Format(getLogPrefix() + format, args);
        Debug.Log(context);
        writeMessageToFile(context, DEBUGLEVEL.INFO);
    }

    public static void LogWarningFormat(string format, params object[] args) {
        if(debugLevel > DEBUGLEVEL.WARNING)
            return;

        string context = string.Format(getLogPrefix() + format, args);
        Debug.LogWarning(context);
        writeMessageToFile(context, DEBUGLEVEL.WARNING);
    }

    public static void LogErrorFormat(string format, params object[] args) {
        if(debugLevel > DEBUGLEVEL.ERROR)
            return;

        string context = string.Format(getLogPrefix() + format, args);
        Debug.LogError(context);
        writeMessageToFile(context, DEBUGLEVEL.ERROR);
    }

    public static void LogException(System.Exception exception) {
        Debug.LogException(exception);
    }

    static void writeMessageToFile(string message, DEBUGLEVEL level) {
        if(writeLevel > level)
            return;
        if(string.IsNullOrEmpty(logFileName))
            logFileName = Application.persistentDataPath + "/" + System.DateTime.Now.Ticks + ".txt";
        using(StreamWriter write = new StreamWriter(logFileName, true, Encoding.UTF8)) {
            write.WriteLine(message);
        }
    }
}

}

